import random

from faker import Faker

from app.models.courseModel import Course
from app.models.coursewareModel import Courseware
from app.models.userModel import User
from manage import app
from tools import db


def add_test_users():
    fake = Faker("zh_CN")
    users = []
    for num in range(0, 10):
        print(num)
        try:
            user = User()
            user.username = fake.user_name()
            user.account_name = fake.name()
            user.password = "123123"
            user.email = fake.email()
            user.role_id = 0
            user.project_id = 1
            users.append(user)
        except Exception as e:
            print(e)
    with app.app_context():
        db.session.add_all(users)
        db.session.commit()
    print("OK")


def add_test_courseware():
    coursewares = []
    fake = Faker("zh_CN")
    for num in range(0, 10):
        print(num)
        try:
            courseware = Courseware()
            courseware.name = fake.file_name()
            courseware.user_id = random.randint(1, 10)
            courseware.type = "1"
            courseware.isSystem = 0
            coursewares.append(courseware)
        except Exception as e:
            print(e)
    with app.app_context():
        db.session.add_all(coursewares)
        db.session.commit()
    print("OK")


def add_test_course():
    courses = []
    fake = Faker("zh_CN")
    for num in range(0, 10):
        print(num)
        try:
            course = Course()
            course.name = fake.file_name()
            course.user_id = random.randint(1, 10)
            course.isSystem = 1
            courses.append(course)
        except Exception as e:
            print(e)
    with app.app_context():
        db.session.add_all(courses)
        db.session.commit()
    print("OK")


if __name__ == "__main__":
    add_test_users()
    add_test_courseware()
    add_test_course()
